package com.wjk.easy.uhf;


import com.android.hdhe.uhf.reader.UhfReader;
import com.android.hdhe.uhf.readerInterface.TagModel;
import com.wjk.easy.utils.UHFUtil;

import java.util.List;

import cn.pda.serialport.Tools;

public class InventoryThread extends Thread {
    private List<TagModel> tagList;
    private boolean runFlag;
    private boolean startFlag;
    byte[] accessPassword = Tools.HexString2Bytes("00000000");
    private UhfReader mUhfReader;

    InventoryThread(UhfReader uhfReader){
        mUhfReader = uhfReader;
        startFlag = true;
        runFlag = true;
    }

    @Override
    public void run() {
        super.run();
        while (runFlag) {
            if (startFlag) {
                tagList = mUhfReader.inventoryRealTime(); //实时盘存
                if(tagList != null && !tagList.isEmpty()){
                    //播放提示音
                    UHFUtil.play(1, 0);
                    onPause();
                    for(TagModel tag:tagList){
                        if(tag == null){
                            String epcStr = "";
//								String epcStr = new String(epc);
                            //addToList(listEPC, epcStr, (byte)-1);
                        }else{
                            String epcStr = Tools.Bytes2HexString(tag.getmEpcBytes(), tag.getmEpcBytes().length);
                            if(listener!=null){
                                listener.onEpcGot(epcStr);
                            }
                        }

                    }
                }
                tagList = null ;
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    void onPause() {
        startFlag = false;
    }

    void onDestroy() {
        startFlag = false;
        runFlag = false;
    }
    private EPCReadListener listener;
    public void setUHFListener(EPCReadListener listener) {
        this.listener = listener;
    }

    public void restart() {
        startFlag = true;
        runFlag = true;
    }
}